package cn.abcsys.cloud.devops.runtime.controller;

import java.util.Map;

import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;

import cn.abcsys.cloud.devops.runtime.bean.GridBean;
import cn.abcsys.cloud.devops.runtime.bean.Result;
import cn.abcsys.cloud.devops.runtime.entity.Cluster;
import cn.abcsys.cloud.devops.runtime.model.ClusterModel;
import cn.abcsys.cloud.devops.runtime.service.ClusterService;
import net.sf.json.JSONObject;


/**
 * 集群管理控制器
 * @author mayunhao
 *
 */
@RestController
@RequestMapping("/cluster")
public class ClusterController {
	private static final Logger LOGGER = Logger.getLogger(ClusterController.class);
	@Autowired
	private ClusterService clusterService;
	@RequestMapping(value = "/list", method = { RequestMethod.GET })
	@ResponseBody
	public GridBean clusterList(@RequestParam Map<String,Object> params){
		if(params.isEmpty()){
			LOGGER.error("请求参数为空");
			return null;
		}else{
			ClusterModel clusterModel = new ClusterModel();
			JSONObject orig = JSONObject.fromObject(params);
			clusterModel = (ClusterModel) JSONObject.toBean(orig,ClusterModel.class);
			return clusterService.getClusterList(clusterModel);
		}
	}
	@RequestMapping(value = "/listAll", method = { RequestMethod.GET })
	@ResponseBody
	public Result listAll(){
		return clusterService.listAll();
	}
	@RequestMapping(value = "/eventList", method = { RequestMethod.GET })
	@ResponseBody
	public GridBean eventList(@RequestParam Map<String,Object> params){
		if(params.isEmpty()){
			LOGGER.error("请求参数为空");
			return null;
		}else{
			ClusterModel clusterModel = new ClusterModel();
			JSONObject orig = JSONObject.fromObject(params);
			clusterModel = (ClusterModel) JSONObject.toBean(orig,ClusterModel.class);
			return clusterService.eventList(clusterModel);
		}
	}
	@RequestMapping(value = "/detail", method = { RequestMethod.GET })
	@ResponseBody
	public Result getDetail(@RequestParam(value = "clusterId", required = true)Integer clusterId){
		if(null==clusterId){
			LOGGER.error("请求参数为空");
			return new Result(false, "请求参数为空");
		}
		return clusterService.getClusterDetail(clusterId);
	}
	@RequestMapping(value = "/create", method = { RequestMethod.GET })
	@ResponseBody
	public Result create(@RequestParam Map<String,Object> params){
		if(params.isEmpty()){
			LOGGER.error("请求参数为空");
			return new Result(false, "请求参数为空");
		}else{
			Cluster cluster = new Cluster();
			try{
				JSONObject orig = JSONObject.fromObject(params);
				cluster = (Cluster)JSONObject.toBean(orig,Cluster.class);
			}catch(Exception e){
				LOGGER.error("请求参数转换错误："+e.getMessage());
				return new Result(false, "请求参数转换错误："+e.getMessage());
			}
			return clusterService.createCluster(cluster);
		}
	}
	@RequestMapping(value = "/modify", method = { RequestMethod.GET })
	@ResponseBody
	public Result modify(@RequestParam Map<String,Object> params){
		if(params.isEmpty()){
			LOGGER.error("请求参数为空");
			return new Result(false, "请求参数为空");
		}else{
			Cluster cluster = new Cluster();
			try{
				JSONObject orig = JSONObject.fromObject(params);
				cluster = (Cluster)JSONObject.toBean(orig,Cluster.class);
			}catch(Exception e){
				LOGGER.error("请求参数转换错误："+e.getMessage());
				return new Result(false, "请求参数转换错误："+e.getMessage());
			}
			return clusterService.updateCluster(cluster);
		}
	}
	@RequestMapping(value = "/remove", method = { RequestMethod.GET })
	@ResponseBody
	public Result remove(@RequestParam(value = "clusterId", required = true)Integer clusterId){
		return clusterService.deleteCluster(clusterId);
	}
}
